//
//  WBBackGroundView.m
//  sinaWeibo
//
//  Created by 李江波 on 2016/11/20.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

#import "WBBackGroundView.h"
#import <Masonry.h>
#import "WBButton.h"
#import <POP.h>

typedef enum : NSUInteger {
    clickBottonTypeSend = 1000,
    clickBottonTypeFriend,
    clickBottonTypeCamera,
    clickBottonTypeLbs,
    clickBottonTypeMusic,
    clickBottonTypeMore
    
} clickBottonType;


@interface WBBackGroundView ()

@property(nonatomic ,strong) NSArray<WBButton *> *btnArray;

@property(nonatomic ,strong) UIViewController *rootVC;

@end

@implementation WBBackGroundView

-(void)moveToVC:(UIViewController *)rootVc{

    _rootVC = rootVc;
    
    [rootVc.view addSubview:self];
}

-(instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
    }
    
    return self;
}

-(void)setupUI{
    
    //制作高斯模糊效果
    UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:self.bounds];
    toolbar.barStyle = UIBarStyleDefault;
    
    UIImageView *signImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"compose_slogan"]];
    
    [self addSubview:toolbar];
    [self addSubview:signImage];
    
    [signImage mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self);
        make.top.equalTo(self).offset(100);
    }];
    
    [self setupBotton];
    
    [self setupAnimation:NO];

}

#pragma mark : - 按钮动画
-(void)setupAnimation:(BOOL)isDismiss {

    if (isDismiss) {
        
        NSEnumerator *enumerator = [_btnArray reverseObjectEnumerator];
        [[enumerator allObjects] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
           
           [self setPopAnimation:obj index:idx isDismiss:isDismiss];
        }];
        
        
    } else {
       
        [_btnArray enumerateObjectsUsingBlock:^(WBButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
           
            [self setPopAnimation:obj index:idx isDismiss:isDismiss];
        }];
        
        
    }

}

-(void)setPopAnimation:(WBButton *)btn index:(NSUInteger)index isDismiss:(BOOL)isDismiss{
    /*1
     let anSpring = POPSpringAnimation(propertyNamed: kPOPLayerPositionY)
     if isDismiss {
     //加多一点，防止pop动画弹出底面一点点
     anSpring?.toValue = btn.center.y + 400
     }else{
     
     anSpring?.toValue = btn.center.y - 350
     }
     anSpring?.beginTime = CACurrentMediaTime() + Double(index) * 0.05
     anSpring?.springBounciness = 10.0
     anSpring?.springSpeed = 10.0
     
     btn.pop_add(anSpring, forKey: nil)
     */

    POPSpringAnimation *anSpring = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionY];
    if (isDismiss) {
        //加多一点，防止pop动画弹出底面一点点
        anSpring.toValue = @(btn.frame.origin.y + 450);
    } else {
        
        anSpring.toValue = @(btn.frame.origin.y - 350);
    }
    anSpring.beginTime = CACurrentMediaTime() + index * 0.05;
    anSpring.springBounciness = 10;
    anSpring.springSpeed = 10;
    
    [btn pop_addAnimation:anSpring forKey:nil];
    
}

#pragma mark : - 设置弹出按钮
-(void)setupBotton {

    NSArray *arrayImageNames = @[@"tabbar_compose_idea",
                                  @"tabbar_compose_friend",
                                  @"tabbar_compose_camera",
                                  @"tabbar_compose_lbs",
                                  @"tabbar_compose_music",
                                  @"tabbar_compose_more"];
    NSArray *arrayTitles = @[@"发布",
                            @"朋友",
                            @"相册",
                            @"位置",
                            @"音乐",
                             @"更多"];
    CGFloat btnWidth = 80;
    CGFloat btnHeight = 110;
    CGFloat margin = (kUISCRRENW - 3 * btnWidth) / 4;
    
    NSMutableArray *arrayM = [NSMutableArray array];
    for (int i = 0; i < arrayImageNames.count; i ++) {
        
        WBButton *btn = [WBButton Button:arrayTitles[i] fontSize:17 normalColor:[UIColor lightGrayColor] ImageName:arrayImageNames[i]];
        
        
        int row = i / 3;
        int col = i % 3;
        CGFloat X = margin + (margin + btnWidth) * col;
        CGFloat Y = (margin + btnHeight) * row + kUISCRRENH;
        
        btn.frame = CGRectMake(X, Y, btnWidth, btnHeight);
        
        btn.tag = 1000 + i;
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:btn];
        [arrayM addObject:btn];
    }
    
    _btnArray = arrayM.copy;
}
#pragma mark : - 按钮点击事件
-(void)clickBtn:(WBButton *)btn{

    switch (btn.tag) {
        case clickBottonTypeSend:
            NSLog(@"点击发送");
            [self modalViewController:btn];
            break;
        case clickBottonTypeFriend:
            NSLog(@"点击朋友");
            [self modalViewController:btn];
            break;
        case clickBottonTypeCamera:
            NSLog(@"点击相册");
            [self modalViewController:btn];
            break;
        case clickBottonTypeLbs:
            NSLog(@"点击位置");
            [self modalViewController:btn];
            break;
        case clickBottonTypeMusic:
            NSLog(@"点击音乐");
            [self modalViewController:btn];
            break;
        case clickBottonTypeMore:
            NSLog(@"点击更多");
            [self modalViewController:btn];
            break;
    }
    
}

-(void)modalViewController:(WBButton *)btn{

    Class clz = NSClassFromString(@"WBSendViewController");
    
    UIViewController *vc = [[clz alloc]init];
    
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
    
    [UIView animateWithDuration:0.5 animations:^{
        
        btn.transform = CGAffineTransformMakeScale(2, 2);
        btn.alpha = 0.8;
        
        for (WBButton *button in self.btnArray) {
            if (button != btn) {
                
                button.transform = CGAffineTransformMakeScale(0.2, 0.2);
                button.alpha = 0.2;
            }
        }
        
    } completion:^(BOOL finished) {
        
        
        [self.rootVC presentViewController:nav animated:YES completion:^{
            
            [self removeFromSuperview];
        }];
        
    }];
    
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self setupAnimation:YES];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self removeFromSuperview];
    });
    
}

@end
